using UnityEditor;
using UnityEngine;
using UnityEngine.UI;

[CustomEditor(typeof(HMI_HorizontalItem), true)]
[CanEditMultipleObjects]
[ExecuteInEditMode]
public class HMI_HorizontalItem_Editor : Editor
{
    private enum Alignment
    {
        上对齐,
        中对齐,
        下对齐,
    }
    private HMI_HorizontalItem mTarget;
    private HorizontalLayoutGroup mLayoutGroup;
    private Text mText;
    private Alignment mAlign;
    private string _curText;

    public void Awake()
    {
        mTarget = target as HMI_HorizontalItem;

        mLayoutGroup = mTarget.GetComponent<HorizontalLayoutGroup>();
        mText = mTarget.transform.Find("Text").GetComponent<Text>();

        _curText = mText.text;
        mAlign = Alignment.上对齐;
        if (mLayoutGroup.childAlignment == TextAnchor.UpperLeft || mLayoutGroup.childAlignment == TextAnchor.UpperCenter || mLayoutGroup.childAlignment == TextAnchor.UpperRight)
            mAlign = Alignment.上对齐;
        else if (mLayoutGroup.childAlignment == TextAnchor.MiddleLeft || mLayoutGroup.childAlignment == TextAnchor.MiddleCenter || mLayoutGroup.childAlignment == TextAnchor.MiddleRight)
            mAlign = Alignment.中对齐;
        else if (mLayoutGroup.childAlignment == TextAnchor.LowerLeft || mLayoutGroup.childAlignment == TextAnchor.LowerCenter || mLayoutGroup.childAlignment == TextAnchor.LowerRight)
            mAlign = Alignment.下对齐;
    }

    public override void OnInspectorGUI()
    {
        EditorGUI.BeginChangeCheck();
        _curText = EditorGUILayout.TextField("文字：", _curText);
        mAlign = (Alignment)EditorGUILayout.EnumPopup("对齐方式：", mAlign);

        mTarget.RTLDontChange = EditorGUILayout.Toggle("RTL布局不变：", mTarget.RTLDontChange);
        if (EditorGUI.EndChangeCheck())
        {
            mText.text = _curText;
            mTarget.SetVoiceText(_curText);

            mLayoutGroup.childAlignment = TextAnchor.UpperLeft;
            if (mAlign == Alignment.上对齐)
                mLayoutGroup.childAlignment = TextAnchor.UpperLeft;
            else if (mAlign == Alignment.中对齐)
                mLayoutGroup.childAlignment = TextAnchor.MiddleLeft;
            else if (mAlign == Alignment.下对齐)
                mLayoutGroup.childAlignment = TextAnchor.LowerLeft;
            EditorUtility.SetDirty(mTarget);
        }
    }
}
